<?php

namespace api\admin;

//统计类
class summary
{
    //获取首页统计数据
    public function getIndex($data)
    {
        $time = time();
        $con = con();
        $user = mysqli_query($con, "select * from user ");
        $user = mysqli_num_rows($user);
        $money = mysqli_query($con, "select IFNULL(SUM(money),0) as money from `order` where status=1");
        $money = mysqli_fetch_all($money, true);
        $money = sprintf("%.2f", $money[0]['money'] / 100);
        $order = mysqli_query($con, "select * from `order`");
        $order = mysqli_num_rows($order);
        $task = mysqli_query($con, "select * from `task`");
        $task = mysqli_num_rows($task);
        mysqli_close($con);
        return out_right([
            'user' => $user,
            'money' => round($money, 2),
            'order' => $order,
            'task' => $task
        ]);
    }

    //获取最近7天日期
    public function getDate()
    {
        $time = time();
        $date=[
            date("m-d", $time-86400*6),
            date("m-d", $time-86400*5),
            date("m-d", $time-86400*4),
            date("m-d", $time-86400*3),
            date("m-d", $time-86400*2),
            '昨日',
            '今日'
        ];
        return out_right($date);
    }

    //获取近7日的订单数和任务数
    public function getMapData(){
        $time=time();
        $start_time=strtotime(date("Y-m-d"));
        $end_time=strtotime(date("Y-m-d"))+86399;

        $con=con();

        $order = mysqli_query($con, "select 
       (select count(*) from `order` where create_time between $start_time and $end_time),
       (select count(*) from `order` where create_time between ($start_time-86400*1) and ($end_time - 86400*1)),
       (select count(*) from `order` where create_time between ($start_time-86400*1) and ($end_time - 86400*2)),
       (select count(*) from `order` where create_time between ($start_time-86400*1) and ($end_time - 86400*3)),
       (select count(*) from `order` where create_time between ($start_time-86400*1) and ($end_time - 86400*4)),
       (select count(*) from `order` where create_time between ($start_time-86400*1) and ($end_time - 86400*5)),
       (select count(*) from `order` where create_time between ($start_time-86400*1) and ($end_time - 86400*6)) ");
        $order=mysqli_fetch_all($order);
        $order=array_reverse($order[0]);


        $task = mysqli_query($con, "select 
       (select count(*) from `task` where create_time between $start_time and $end_time),
       (select count(*) from `task` where create_time between ($start_time-86400*1) and ($end_time - 86400*1)),
       (select count(*) from `task` where create_time between ($start_time-86400*1) and ($end_time - 86400*2)),
       (select count(*) from `task` where create_time between ($start_time-86400*1) and ($end_time - 86400*3)),
       (select count(*) from `task` where create_time between ($start_time-86400*1) and ($end_time - 86400*4)),
       (select count(*) from `task` where create_time between ($start_time-86400*1) and ($end_time - 86400*5)),
       (select count(*) from `task` where create_time between ($start_time-86400*1) and ($end_time - 86400*6)) ");
        $task=mysqli_fetch_all($task);
        $task=array_reverse($task[0]);

        mysqli_close($con);
        return out_right([
            'expectedData' => $order,
            'actualData' => $task
        ]);
    }

}